package com.goro;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectOne;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;

import com.joro.Customer;
import com.joro.Magazine;
import com.joro.Subscription;
import com.joro.Supplier;
import com.joro.ejb.CustomerFacadeLocal;
import com.joro.ejb.MagazineFacadeLocal;
import com.joro.ejb.SubscriptionFacadeLocal;
import com.joro.ejb.SupplierFacadeLocal;
import java.util.Map.Entry;

public class SubscriptionEditBean {

	@EJB
	protected SubscriptionFacadeLocal subscriptionDao;
	@EJB
	protected CustomerFacadeLocal customerDao;
	@EJB
	protected MagazineFacadeLocal magazineDao;
	@EJB
	protected SupplierFacadeLocal supplierDao;
	
	private Subscription subscription;
	private int subscriptionId;
	private int customerId;
	private String action;
	private String magazineId = new String();
	private String supplierId = new String();
	
	UISelectOne supplierChooserComponent;
	
	
	

	public UISelectOne getSupplierChooserComponent() {
		return supplierChooserComponent;
	}

	public void setSupplierChooserComponent(UISelectOne supplierChooserComponent) {
		this.supplierChooserComponent = supplierChooserComponent;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public String getMagazineId() {
		return magazineId;
	}

	public void setMagazineId(String magazineId) {
		this.magazineId = magazineId;
	}

	public SubscriptionEditBean() {
		subscription = new Subscription();
	}
	
	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		if(customerId > 0){
			this.customerId = customerId;
			if(this.subscriptionId <= 0){
				Customer customer = customerDao.getCustomerById(customerId);
				if(customer != null){
					this.subscription.setCustomer(customer);
				}
			}
		}
	}

	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;

		if (this.action != null && this.action.equals("edit")) {
			this.getSubscriptionData();
		}
	}

	

	public String getSubscriptionData() {
		if (this.subscriptionId > 0) {
			// Init the subscription member
			Subscription tempSubscription = subscriptionDao.getSubscriptionById(subscriptionId);
			if (tempSubscription != null) {
				this.subscription = tempSubscription;
				this.supplierId = String.valueOf(tempSubscription.getSupplier().getId());
				this.magazineId = String.valueOf(tempSubscription.getMagazine().getId());
				this.customerId = tempSubscription.getCustomer().getId();
			}
			
		}
		return null;
	}

	public int getSubscriptionId() {
		return subscriptionId;
	}

	public void setSubscriptionId(int subscriptionId) {
		this.subscriptionId = subscriptionId;
		this.subscription.setId(subscriptionId);

	}

	public Subscription getSubscription() {
		return subscription;
	}

	public void setSubscription(Subscription subscription) {
		this.subscription = subscription;
	}

	public String saveSubscription() {	
		if(this.magazineId != null){
			Magazine magazine = magazineDao.getMagazineById(Integer.parseInt(this.magazineId));
			if(magazine != null)
				this.subscription.setMagazine(magazine);
		}
		if(this.supplierId != null){
			Supplier supplier = supplierDao.getSupplierById(Integer.parseInt(this.supplierId));
			if(supplier != null)
				this.subscription.setSupplier(supplier);
		}
		if(this.customerId > 0){
			Customer customer = customerDao.getCustomerById(customerId);
			if(customer != null)
				this.subscription.setCustomer(customer);
		}
		subscriptionDao.saveSubscription(this.subscription);		
		return "success";
	}

	public String deleteSubscription() {
		this.getSubscriptionData();  
		//Fill customerId, because it is needed for redirect actions
		if(this.customerId <= 0 && this.subscription != null){
			this.customerId = this.subscription.getCustomer().getId();
		}
		subscriptionDao.deleteSubscription(subscription);		
		return "successful_subscription_delete";
	}

	public String cancel() {
		return "success";
	}

	
	public void processValueChange(ValueChangeEvent event)
			throws AbortProcessingException {
		int magazineID = Integer.parseInt(event.getNewValue().toString());
		Collection<Supplier> suppliersByMagazineId = supplierDao.getSuppliersByMagazineId(magazineID);
		supplierChooserComponent.getChildren().clear();
		for (Supplier supplier : suppliersByMagazineId) {
			UISelectItem item = new UISelectItem();
			item.setItemValue(supplier.getId());
			item.setItemLabel(supplier.getName());
			supplierChooserComponent.getChildren().add(item);
		}
		
	}
	public String getInitMagazineSuppliers(){
		String result = "<script>";
		Map<Integer, Collection<Integer>> magazineMap = new HashMap<Integer, Collection<Integer>>();
		for(Supplier supplier:supplierDao.getSuppliers()){
			for(Magazine magazine:supplier.getMagazines()){
				if(magazineMap.get(magazine.getId()) == null){
					magazineMap.put(magazine.getId(),new ArrayList<Integer>());
				}
				magazineMap.get(magazine.getId()).add(supplier.getId());
			}
		}		
		
		for(Entry<Integer, Collection<Integer>> entry:magazineMap.entrySet()){
			result += " gMagazineMap[" + entry.getKey() + "] = new Array(";
			boolean first = true;
			for(Integer i : entry.getValue()){
				if(!first){
					result += ",";
				}else{
					first = false;
				}
				result += "'" + i.toString() + "'";
			}
			result += ");";
		}
		result += "initSuppliers();</script>";
		return result;
	}
}
